﻿//GB/Z 29014.3 ISYC 307-04-可转位刀片柄连接T形槽立铣刀

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class class_ISYC_307_04
    {
        public static void ISYC_307_F72(Dictionary<string, Object> data)
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //基本参数-------------------------------------------------------------------------------------------------
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            //赋值参数--------------------------------------------------------------------------------------------------
            /*double scale = 1;
            double OAL = 41.88 * scale;
            double LPR = 26.23 * scale;
            double CW = 6.18 * scale;
            double DC = 14.86 * scale;
            double BD = 13.53 * scale;
            double LS = 20.06 * scale;
            double CDX = 4.24 * scale;
            double DCONMS = 7.55 * scale;
            double LU = 18.84 * scale;
            double DN = 6.38 * scale;
            double KAPR = 90;*/

            double scale = Convert.ToDouble(data["scale"]);
            double OAL = Convert.ToDouble(data["oal"]) * scale;
            double LPR = Convert.ToDouble(data["lpr"]) * scale;
            double CW = Convert.ToDouble(data["cw"]) * scale;
            double DC = Convert.ToDouble(data["dc"]) * scale;
            double BD = Convert.ToDouble(data["bd"]) * scale;
            double LS = Convert.ToDouble(data["ls"]) * scale;
            double CDX = Convert.ToDouble(data["cdx"]) * scale;
            double DCONMS = Convert.ToDouble(data["dconms"]) * scale;
            double LU = Convert.ToDouble(data["lu"]) * scale;
            double KAPR = Convert.ToDouble(data["kapr"]);

            //点---------------------------------------------------------------------------------------------
            //第一部分
            //中心线
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + OAL, v1.Y);

            //轮廓
            Vector2 v4 = new Vector2(v1.X + (0.58 / 20.06) * LS, v1.Y + DCONMS / 2.0);
            Vector2 v3 = fun.Inters(v1, fun.Polar(v1, 90 * pi / 180, DCONMS), v4, fun.Polar(v4, -135 * pi / 180, OAL));
            Vector2 v5 = new Vector2(v3.X + LS, v4.Y);
            Vector2 v6 = new Vector2(v5.X + (0.58 / 20.06) * LS, v5.Y - (0.58 / 20.06) * LS);
            Vector2 v7 = new Vector2(v2.X - CW + (0.49 / 6.18) * CW, v6.Y);
            Vector2 v8 = new Vector2(v7.X, v2.Y + BD / 2.0);
            Vector2 v9 = new Vector2(v8.X + (1.99 / 6.18) * CW, v8.Y);
            Vector2 v10 = new Vector2(v2.X, v2.Y + DC / 2.0);
            Vector2 v11 = new Vector2(v10.X, v10.Y - (3.16 / 13.53) * BD);
            Vector2 v12 = new Vector2(v9.X, v11.Y);
            Vector2 v13 = new Vector2(v12.X, v10.Y);
            Vector2 v14 = fun.Inters(v10, v12, v13, v11);
            Vector2 v15 = new Vector2(v13.X + (0.47 / 6.18) * CW, v13.Y - (0.4 / 14.86) * DC);
            Vector2 v16 = new Vector2(v10.X - (0.47 / 6.18) * CW, v13.Y - (0.4 / 14.86) * DC);
            Vector2 v17 = new Vector2(v11.X - (0.47 / 6.18) * CW, v11.Y + (0.4 / 14.86) * DC);
            Vector2 v18 = new Vector2(v12.X + (0.47 / 6.18) * CW, v11.Y + (0.4 / 14.86) * DC);
            Vector2 v19 = new Vector2(v11.X - (0.49 / 6.18) * CW, v11.Y);
            Vector2 v20 = new Vector2(v19.X, v2.Y - BD / 2.0);
            Vector2 v21 = new Vector2(v8.X, -v8.Y);
            Vector2 v22 = new Vector2(v7.X, -v7.Y);
            Vector2 v23 = new Vector2(v20.X - fun.CalDistance(v8, v9), v20.Y);
            Vector2 v24 = new Vector2(v23.X, v23.Y - fun.CalDistance(v9, v13));
            Vector2 v25 = new Vector2(v2.X - CW, v2.Y - DC / 2.0);
            Vector2 v26 = new Vector2(v25.X, v25.Y + fun.CalDistance(v10, v11));
            Vector2 v27 = fun.Inters(v26, fun.Polar(v26, 0 * pi / 180, OAL), v21, v22);
            Vector2 v28 = new Vector2(v6.X, -v6.Y);
            Vector2 v29 = new Vector2(v5.X, -v5.Y);
            Vector2 v30 = new Vector2(v4.X, -v4.Y);
            Vector2 v31 = new Vector2(v3.X, -v3.Y);
            Vector2 v32 = new Vector2(v7.X, v7.Y + (0.7 / 14.86) * DC);
            Vector2 v33 = fun.Inters(v32, fun.Polar(v32, 0 * pi / 180, OAL), v19, v20);
            Vector2 v34 = new Vector2(v4.X + (9.2 / 20.06) * LS, v4.Y + (4.79 / 14.86) * DC);
            Vector2 v35 = new Vector2(v34.X + (2.94 / 20.06) * LS, v34.Y);
            Vector2 v64 = new Vector2(v1.X + (15.64 / 20.06) * LS, v1.Y);
            Vector2 v36 = fun.Inters(v64, fun.Polar(v64, 90 * pi / 180, OAL), v35, fun.Polar(v35, -38 * pi / 180, OAL));
            Vector2 v37 = new Vector2(v36.X, -v36.Y);
            Vector2 v38 = new Vector2(v35.X, -v35.Y);
            Vector2 v39 = new Vector2(v34.X, -v34.Y);
            Vector2 v40 = new Vector2(v2.X + (30 / 41.88) * OAL, v2.Y);
            Vector2 v41 = new Vector2(v40.X + BD * 1.2, v40.Y);
            Vector2 v42 = new Vector2(v40.X + (fun.CalDistance(v40, v41) / 2.0), v40.Y);
            Vector2 v43 = fun.Inters1(v33, fun.Polar(v33, 0 * pi / 180, OAL), new Arc(v42, BD / 2.0, 0, 360), 0);
            Vector2 v44 = new Vector2(v42.X, v42.Y + DC / 2.0);
            Vector2 v45 = new Vector2(v42.X + (0.86 / 13.05) * DC, v44.Y - (0.5 / 13.05) * DC);
            Vector2 v45_1 = new Vector2(v45.X, v17.Y);
            Vector2 v44_1 = fun.Inters(v43, fun.Polar(v43, 4 * pi / 180, OAL), v45_1, fun.Polar(v45_1, -162 * pi / 180, OAL));
            Vector2 v46 = fun.Inters(v44, fun.Polar(v44, -90 * pi / 180, OAL), v44_1, v45_1);
            Vector2 v47 = new Vector2(v44.X, v44.Y - (2.54 / 13.05) * DC);
            Vector2 v48 = new Vector2(v47.X, v9.Y);
            Vector2 v49 = fun.Inters(v48, fun.Polar(v48, 21 * pi / 180, OAL), v45, fun.Polar(v45, -90 * pi / 180, OAL));
            Vector2 v50 = new Vector2(v44.X, -v44.Y);
            Vector2 v51 = fun.rotatePoint_Changed(v42, v45, 180);
            Vector2 v52 = fun.rotatePoint_Changed(v42, v45_1, 180);
            Vector2 v53 = fun.rotatePoint_Changed(v42, v44_1, 180);
            Vector2 v54 = fun.rotatePoint_Changed(v42, v43, 180);
            Vector2 v55 = fun.rotatePoint_Changed(v42, v47, 180);
            Vector2 v56 = fun.rotatePoint_Changed(v42, v49, 180);
            Vector2 v57 = fun.rotatePoint_Changed(v42, v48, 180);
            Vector2 v58 = fun.getPointForArcandLine(v52, v51, v42, new Arc(v42, BD / 2.0, 0, 360));
            Vector2 v59 = new Vector2(v50.X, v42.Y - BD / 2.0);
            Vector2 v60 = fun.rotatePoint_Changed(v42, v59, 180);
            Vector2 v61 = fun.rotatePoint_Changed(v42, v58, 180);
            Vector2 v46_1 = fun.rotatePoint_Changed(v42, v46, 180);
            Vector2 v62 = new Vector2(v30.X - (4 / 24.07) * LS, v30.Y - (4.67 / 11.13) * DCONMS);
            Vector2 v63 = new Vector2(v8.X + (14.17 / 51.79) * OAL, v8.Y + (18.83 / 14.86) * DC);


            //作图部分-----------------------------------------------------------------------
            //中心线
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.Center;
            line1.LinetypeScale = 5;
            dxf.AddEntity(line1);

            /*Line line2 = new Line(v40, v41);
            line2.Layer = centre;
            line2.Linetype = Linetype.Center;
            line2.LinetypeScale = 5;
            dxf.AddEntity(line2);

            Line line3 = new Line(v44, v50);
            line3.Layer = centre;
            line3.Linetype = Linetype.Center;
            line3.LinetypeScale = 5;
            dxf.AddEntity(line3);*/

            //第一部分
            //轮廓
            LwPolyline polyline1 = new LwPolyline();
            polyline1.Vertexes.Add(new LwPolylineVertex(v1));
            polyline1.Vertexes.Add(new LwPolylineVertex(v3));
            polyline1.Vertexes.Add(new LwPolylineVertex(v4));
            polyline1.Vertexes.Add(new LwPolylineVertex(v5));
            polyline1.Vertexes.Add(new LwPolylineVertex(v6));
            polyline1.Vertexes.Add(new LwPolylineVertex(v7));
            polyline1.Vertexes.Add(new LwPolylineVertex(v8));
            polyline1.Vertexes.Add(new LwPolylineVertex(v9));
            fun.PolyFill(dxf, polyline1, 0.3, contour);

            LwPolyline polyline2 = new LwPolyline();
            polyline2.Vertexes.Add(new LwPolylineVertex(v10));
            polyline2.Vertexes.Add(new LwPolylineVertex(v11));
            polyline2.Vertexes.Add(new LwPolylineVertex(v12));
            polyline2.Vertexes.Add(new LwPolylineVertex(v13));
            polyline2.Vertexes.Add(new LwPolylineVertex(v10));
            fun.PolyFill(dxf, polyline2, 0.3, contour);

            LwPolyline polyline3 = new LwPolyline();
            polyline3.Vertexes.Add(new LwPolylineVertex(v19));
            polyline3.Vertexes.Add(new LwPolylineVertex(v20));
            polyline3.Vertexes.Add(new LwPolylineVertex(v21));
            polyline3.Vertexes.Add(new LwPolylineVertex(v22));
            polyline3.Vertexes.Add(new LwPolylineVertex(v28));
            polyline3.Vertexes.Add(new LwPolylineVertex(v29));
            polyline3.Vertexes.Add(new LwPolylineVertex(v30));
            polyline3.Vertexes.Add(new LwPolylineVertex(v31));
            polyline3.Vertexes.Add(new LwPolylineVertex(v1));
            fun.PolyFill(dxf, polyline3, 0.3, contour);

            LwPolyline polyline4 = new LwPolyline();
            polyline4.Vertexes.Add(new LwPolylineVertex(v15));
            polyline4.Vertexes.Add(new LwPolylineVertex(v16));
            polyline4.Vertexes.Add(new LwPolylineVertex(v17));
            polyline4.Vertexes.Add(new LwPolylineVertex(v18));
            polyline4.Vertexes.Add(new LwPolylineVertex(v15));
            fun.PolyFill(dxf, polyline4, 0.3, contour);

            LwPolyline polyline5 = new LwPolyline();
            polyline5.Vertexes.Add(new LwPolylineVertex(v32));
            polyline5.Vertexes.Add(new LwPolylineVertex(v33));
            fun.PolyFill(dxf, polyline5, 0.3, contour);

            LwPolyline polyline6 = new LwPolyline();
            polyline6.Vertexes.Add(new LwPolylineVertex(v4));
            polyline6.Vertexes.Add(new LwPolylineVertex(v30));
            fun.PolyFill(dxf, polyline6, 0.3, contour);

            LwPolyline polyline7 = new LwPolyline();
            polyline7.Vertexes.Add(new LwPolylineVertex(v5));
            polyline7.Vertexes.Add(new LwPolylineVertex(v29));
            fun.PolyFill(dxf, polyline7, 0.3, contour);

            LwPolyline polyline8 = new LwPolyline();
            polyline8.Vertexes.Add(new LwPolylineVertex(v6));
            polyline8.Vertexes.Add(new LwPolylineVertex(v28));
            fun.PolyFill(dxf, polyline8, 0.3, contour);

            LwPolyline polyline9 = new LwPolyline();
            polyline9.Vertexes.Add(new LwPolylineVertex(v7));
            polyline9.Vertexes.Add(new LwPolylineVertex(v22));
            fun.PolyFill(dxf, polyline9, 0.3, contour);

            LwPolyline polyline10 = new LwPolyline();
            polyline10.Vertexes.Add(new LwPolylineVertex(v23));
            polyline10.Vertexes.Add(new LwPolylineVertex(v24));
            polyline10.Vertexes.Add(new LwPolylineVertex(v25));
            polyline10.Vertexes.Add(new LwPolylineVertex(v26));
            polyline10.Vertexes.Add(new LwPolylineVertex(v27));
            fun.PolyFill(dxf, polyline10, 0.3, contour);

            fun.drawcircle(dxf, v14, (0.5 / 6.18) * CW, 0.3, contour);

            //虚线
            LwPolyline polyline13 = new LwPolyline();
            polyline13.Vertexes.Add(new LwPolylineVertex(v34));
            polyline13.Vertexes.Add(new LwPolylineVertex(v35));
            polyline13.Vertexes.Add(new LwPolylineVertex(v36));
            polyline13.Vertexes.Add(new LwPolylineVertex(v37));
            polyline13.Vertexes.Add(new LwPolylineVertex(v38));
            polyline13.Vertexes.Add(new LwPolylineVertex(v39));
            polyline13.Linetype = Linetype.Dashed;
            polyline13.LinetypeScale = 5;
            polyline13.Layer = refin;
            dxf.AddEntity(polyline13);

            //第二部分
            //轮廓
            /*LwPolyline polyline14 = new LwPolyline();
            polyline14.Vertexes.Add(new LwPolylineVertex(v43));
            polyline14.Vertexes.Add(new LwPolylineVertex(v44_1));
            polyline14.Vertexes.Add(new LwPolylineVertex(v45_1));
            polyline14.Vertexes.Add(new LwPolylineVertex(v45));
            polyline14.Vertexes.Add(new LwPolylineVertex(v44));
            polyline14.Vertexes.Add(new LwPolylineVertex(v46));
            fun.PolyFill(dxf, polyline14, 0.3, contour);

            LwPolyline polyline17 = new LwPolyline();
            polyline17.Vertexes.Add(new LwPolylineVertex(v54));
            polyline17.Vertexes.Add(new LwPolylineVertex(v53));
            polyline17.Vertexes.Add(new LwPolylineVertex(v52));
            polyline17.Vertexes.Add(new LwPolylineVertex(v51));
            polyline17.Vertexes.Add(new LwPolylineVertex(v50));
            polyline17.Vertexes.Add(new LwPolylineVertex(v46_1));
            fun.PolyFill(dxf, polyline17, 0.3, contour);

            var arc1 = fun.DrawArc(v43, v42, v58);
            fun.drawBArc(dxf, arc1, 0.3, contour);

            var arc2 = fun.DrawArc(v54, v42, v61);
            fun.drawBArc(dxf, arc2, 0.3, contour);*/

            //标注---------------------------------------------------------------------------
            double height = 2 * scale;
            double arrowsize = 1 * scale;
            double exline = 0.5 * scale;

            //坐标系
            /*fun.Axis(dxf, v1, 180, 270, "", "", "PCS", 0.2 * scale, height * 0.5, 0);
            fun.Axis(dxf, v64, 180, 270, "", "", "MCS", 0.2 * scale, height * 0.5, 0);
            fun.Axis(dxf, v62, 180, 270, "Z", "Y", "X", 0.2 * scale, height * 0.5, 0);*/

            //DCONMS
            fun.dimrot1(dxf, v4, v30, 90, LS * 0.3, 0.1, height, arrowsize, exline, "DCONMS", "", "");

            //LS
            fun.dimrot1(dxf, v31, v29, 180, DCONMS * 2, 0.1, height, arrowsize, exline, "LS", "", "");

            //OAL
            fun.dimrot1(dxf, v3, v10, 0, DCONMS * 4, 0.1, height, arrowsize, exline, "OAL", "", "");

            //LPR
            fun.dimrot1(dxf, v36, v10, 0, DCONMS * 2.5, 0.1, height, arrowsize, exline, "LPR,LTA", "", "");

            //LU
            fun.dimrot1(dxf, fun.Polar(v6, 0 * pi / 180, (2.39 / 15.55) * fun.CalDistance(v6, v7)), v10, 0, DCONMS * 1.5, 0.1, height, arrowsize, exline, "LU", "", "");

            //DC
            fun.dimrot1(dxf, v10, v24, -90, OAL * 0.3, 0.1, height, arrowsize, exline, "DC", "", "");

            //BD
            fun.dimrot1(dxf, v9, v20, -90, OAL * 0.2, 0.1, height, arrowsize, exline, "BD", "", "");

            //CW
            fun.dimrot1(dxf, v25, v11, 180, DCONMS * 2.5, 0.1, height, arrowsize, exline, "CW", "", "");

            //DN
            fun.dimrot1(dxf, fun.Polar(v6, 0, (6.87 / 15.55) * fun.CalDistance(v6, v7)), fun.Polar(v28, 0, (6.87 / 15.55) * fun.CalDistance(v6, v7)), -90, 0.1, 0.1, height, arrowsize, exline, "DN", "", "");

            //CDX
            fun.dimrot1(dxf, v22, v25, 90, CW * 0.3, 0.1, height, arrowsize, exline, "CDX", "", "");

            //角度标注
            //KAPR
            fun.jdbz2(dxf, new Line(v10, fun.Polar(v10, 90 * pi / 180, 0.1)), new Line(v10, fun.Polar(v10, (90 + KAPR) * pi / 180, 0.1)), DCONMS * 0.8, height, arrowsize, exline, "KAPR", "", "");

            //箭头
            //FDP
            //fun.jt1(dxf, new Vector2(v10.X + (7.13 / 41.88) * OAL, v10.Y + (13.97 / 14.86) * DC), -90, 5, 1, "FDP", height);

            //A
            //fun.jt1(dxf, new Vector2(v40.X - (15 / 41.88) * OAL, v40.Y), 0, 5, 1, "A", height);

            //字符
            //fun.text(dxf, new Vector2(v44.X - (2.45 / 11.13) * DCONMS, v44.Y + (2.24 / 11.13) * DCONMS), height, "View A");

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
